package com.digicheck.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.digicheck.dao.ReportDAO;
import com.digicheck.dto.ReportDTO;

@Repository
public class ReportDAOImpl implements ReportDAO{
	
	private static final Logger log = Logger.getLogger(ReportDAOImpl.class);

	protected JdbcTemplate jdbcTemplate;
	
	public List<ReportDTO> getReportList(String date, int type, int idSuc) throws Exception {
		List<ReportDTO> suppliersList = null;
		StringBuilder sql = new StringBuilder();
		sql.append("select doc_archivo from documento ");
		sql.append("where chq_id in (select min(chq_id) from lote inner join cheque ");
		sql.append("on lote.lot_id = cheque.lot_id where lot_tipo = ? and suc_id = ? and  ");
		sql.append("convert( varchar, lot_fecha, 103 ) = ? group by lote.lot_id) ");
		try{
			suppliersList = this.jdbcTemplate.query(sql.toString(), new Object[]{type, idSuc, date}, new RowMapper<ReportDTO>() {
				public ReportDTO mapRow(ResultSet rs, int arg1) throws SQLException {
					ReportDTO dto = new ReportDTO();
					dto.setImage(rs.getBytes(""));
					return dto;
				}
			});
		}catch(Exception exception){
			log.error(exception);
		}		
		return suppliersList;
		
	}

	@Autowired
	public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
}
